//
//  CYDataSourceDelegate.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/6/9.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYDataSourceDelegate.h"
#import "CYHeaderCRView.h"
/**
 * @pragma CYDataSource
 */
@interface CYDataSource(){
    NSString *_identifier;
    cellback _myblock;
}
@end

@implementation CYDataSource

- (instancetype)initWithData:(NSArray *)data identifier:(NSString *)identifier block:(cellback)block{
    self = [super init];
    if (self) {
        _isMulti = YES;
        _dataArr = [NSMutableArray arrayWithArray:data];
        _identifier = identifier;
        _myblock = block;
        
    }
    return self;
}
//- (instancetype)initWithDict:(NSDictionary *)dict identifier:(NSString *)identifier block:(cellback)block{
//    self = [super init];
//    if (self) {
//        _isMulti = YES;
//        _dataDic = (NSMutableDictionary *)dict;
//        _identifier = identifier;
//        _myblock = block;
//
//    }
//    return self;
//
//}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    //    NSAssert((_dataArr || _dataDic), @"_dataArr和_dataDic都有值-->numberOfSections");
    //    NSAssert([_dataDic count] <= 1, @"_dataDic有多对键值队-->numberOfSections");
    //    if ([_dataArr count] == 0 && !_dataDic)
    //        return 0;
    if ([_dataArr count] == 0  || !_dataArr)
        return 0;
    else if ([_dataArr[0] isKindOfClass:[NSArray class]])
        return _dataArr.count;
    else
        return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //    if (_dataDic)
    //        return [[[_dataDic allKeys] firstObject] integerValue];
    if ([_dataArr[0] isKindOfClass:[NSArray class]])
        return [_dataArr[section] count];
    else
        return _dataArr.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *identifierStr = (!_isMulti)?[NSString stringWithFormat:@"%@%lu%lu",_identifier,indexPath.section,indexPath.row]:_identifier;
    //定义唯一标识，系统会根据这个标识，到重用队列中取cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierStr];
    //如果重用队列中的cell不存在,则初始化一个
    
    if (!cell) {
        Class cellClass = NSClassFromString(_identifier);
        cell = [[cellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifierStr];
    }
    
    /*判断是否是二维数组，如果是二维数粗，则表示有多个section，则需要取数据的最里层的数组*/
    //    if (_dataArr && !_dataDic)
    id data = ([_dataArr[indexPath.section] isKindOfClass:[NSArray class]])?_dataArr[indexPath.section][indexPath.row]:_dataArr[indexPath.row];
    //    else if(_dataDic && !_dataArr)
    //        data = [_dataDic allValues][indexPath.section][indexPath.row];
    _myblock(cell, data);/*绑定数据*/
    return cell;
}


@end

/**
 * @pragma CYDelegate
 */
@interface CYDelegate(){
    selectedCell _mySelected;
    CGFloat _size;//字体大小
    CGPoint old_offset_point;//拖动tableview的上一次坐标
}

@end

@implementation CYDelegate
- (instancetype)initWithCellHeight:(CGFloat )height views:(NSArray *)views block:(selectedCell)block{
    self = [super init];
    if (self) {
        _height = height;
        _views = views;
        _mySelected = block;
        self.deselect = YES;
    }
    return self;
}

- (instancetype)initWithAutoHeight:(NSArray *)array views:(NSArray *)views  block:(selectedCell)block{
    self = [super init];
    if (self) {
        _cellHeights = array;
        _views = views;
        _mySelected = block;
        self.deselect = YES;
    }
    return self;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_cellHeights.count > 0 && _cellHeights.count != 2)
        return AL([_cellHeights[indexPath.row] integerValue]);
    else if (_cellHeights.count == 2)
        return (indexPath.row == 0)?AL([_cellHeights[0] integerValue]):AL([_cellHeights[1] integerValue]);
    else
        return (_height <= 0)?AL(44):_height;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (_views.count > 0 && _views &&[[_views[section] allKeys] containsObject:@"headerHeight"]) {
        
        NSAssert([[_views[section] allKeys] containsObject:@"headerHeight"], @"字典没有key-->headerHeight");
        //        NSAssert((_views.count < section  +1), @"views视图数组与cell的section数目不一致");
        return (CGFloat)[_views[section][@"headerHeight"] floatValue];
        
    }
    return 0.1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    //    NSAssert([[_views[section] allKeys] containsObject:@"footerHeight"], @"字典没有key-->footerHeight");
    if (_views.count > 0 && _views && [[_views[section] allKeys] containsObject:@"footerHeight"]) {
        return (CGFloat)[_views[section][@"footerHeight"] floatValue];
        
    }
    return 0.1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    //    NSAssert([[_views[section] allKeys] containsObject:@"headerView"], @"字典没有key-->headerView");
    if (_views.count > 0 && _views &&[[_views[section] allKeys] containsObject:@"headerView"]) {
        return _views[section][@"headerView"];
    }
    return nil;
}

- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    //    NSAssert([[_views[section] allKeys] containsObject:@"footerView"], @"字典没有key-->footerView");
    if (_views.count > 0 && _views && [[_views[section] allKeys] containsObject:@"footerView"]) {
        return _views[section][@"footerView"];
        
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_mySelected)
        _mySelected(indexPath);
    if (_deselect)//
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

//修改delete按钮文字为:"删除";
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}
/*监听tableview滚动*/
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.delegate scrollDelegate:scrollView];
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    old_offset_point = CGPointMake(scrollView.contentOffset.x, scrollView.contentOffset.y);
}
@end


/**
 * @pragma CYCollectionDataSource
 */
@interface CYCollectionDataSource(){
    NSString *_identifier;  /*自定义cell标识符*/
    NSArray *_views;
    collectionCellBack _myblock;
    //    Number _myNum;         /*返回中奖的号码*/
}

@end
@implementation CYCollectionDataSource

- (instancetype)initWithData:(NSMutableArray *)data identifier:(NSString *)identifier views:(NSArray *)views block:(collectionCellBack)block{
    self = [super init];
    if (self) {
        _identifier = identifier;
        _dataArr = data;
        _views = views;
        _myblock = block;
        //    _myNum = number;
    }
    return self;
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    if ([_dataArr[0] isKindOfClass:[NSArray class]])
        return _dataArr.count;
    else
        return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if ([_dataArr[0] isKindOfClass:[NSArray class]])
        return [_dataArr[section] count];
    else
        return _dataArr.count;
}

- ( UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:_identifier forIndexPath:indexPath];
    //如果重用队列中的cell不存在,则初始化一个
    if (!cell) {
        Class cellClass = NSClassFromString(_identifier);
        cell = [[cellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:_identifier];
    }
    _myblock(cell, _dataArr[indexPath.section][indexPath.row], indexPath);
    
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        if (kind  == UICollectionElementKindSectionHeader)
            return [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        else if (kind == UICollectionElementKindSectionFooter)
            return [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];
    }
    return nil;
}
@end


/**
 * @pragma CYCollectionDelegate
 */
@interface CYCollectionDelegate()
{
    NSArray *_dataArr;
    selectedCell _myblock;
    
}

@end
@implementation CYCollectionDelegate

- (instancetype) initWithData:(NSArray *)data block:(selectedCell)block{
    self = [super init];
    if (self) {
        _myblock = block;
        _dataArr = data;
    }
    return self;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    _myblock(indexPath);
    DeLog(@"选中collectionview第  --> %ld 行  第-->%ld 列",indexPath.section, indexPath.row);
    
}

#pragma mark - UIScrollViewDelegate
/*监听collectionview滚动*/
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
  [self.delegate scrollDelegate:scrollView];
 
}


#pragma mark  - UICollectionViewDelegateFlowLayout <UICollectionViewDelegate>事件
/*返回cell的size，*/
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0 || indexPath.section == 1)
        return CGSizeMake(SCREEN_WIDTH / 2.0, AL(PT(120)));
    
    return CGSizeZero;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    if (section == 0)
        return CGSizeMake(SCREEN_WIDTH, AL(PT(150 + 60 + 378 / 750.0 * SCREEN_WIDTH * 2 + 10)));
    else if (section ==1)
        return CGSizeZero;
    
    return CGSizeZero;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    if (section == 0)
        
        return CGSizeMake(SCREEN_WIDTH, AL(PT(60)));
    
    else
        return CGSizeZero;
    
    return CGSizeZero;
}

//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
//    if (indexPath.section == 0) {
//
//
//}
@end


